const Log = require("../model/Log")

module.exports = {
  async getLogList(req, res, next) {
    const pageSize = 10 // 每页条数
    const pageNum = Number(req.query.p ? req.query.p : 1) // 当前页
    const list = await Log.getLogList(pageNum, pageSize)
    const total = (await Log.getLogCount())[0].total
    req.pageList = {
      pageNum,
      pageSize,
      total,
      pages: Math.ceil(total / pageSize),
      list
    }
    next()
  },
  async insertLog(req) {
    if(req.log) {
      let log = {
        handle: req.log.handle,
        ip: req.ip.split(':')[3] ? req.ip.split(':')[3] : 'localhost',
        time: new Date()
      }
      Log.insertLog(log)
    }
  }
}